# cat << ►_◄ > /dev/null
#
# -- general -------------------------------------------------------------------
# list-keys

set -g default-terminal "screen-256color" # colors!
setw -g xterm-keys on
set -s escape-time 0                      # fastest command sequences
#set -sg repeat-time 600                   # increase repeat timeout
#set -s quiet on                           # disable various messages

# unbind Ctrl+b with prefix
unbind C-b
set -g prefix C-f
bind C-f send-prefix

#set -g prefix2 C-f                        # GNU-Screen compatible prefix
#bind C-f send-prefix -2

set -g buffer-limit 10                    # number of buffers
set -g history-limit 1000                 # boost history

# edit configuration
# key: prefix e
bind e new-window -n '~/.tmux.conf' "sh -c '\${EDITOR:-vim} ~/.tmux.conf && tmux source ~/.tmux.conf && tmux display \"~/.tmux.conf sourced\"'"

# reload configuration
# key: prefix r
bind r source-file ~/.tmux.conf \; display '~/.tmux.conf reloaded'

# -- display -------------------------------------------------------------------

set -g base-index 1         # start windows numbering at 1
setw -g pane-base-index 1   # make pane numbering consistent with windows

# move the window to left side
bind-key -n F1 swap-window -d -t -1
# move the window to right side
bind-key -n F2 swap-window -d -t +1

# rename window to reflect current program
setw -g automatic-rename off

# renumber windows when a window is closed
set -g renumber-windows on

set -g set-titles on                        # set terminal title
#set -g set-titles-string '#h ❐ #S ● #I #W'
set-option -g set-titles-string "#(whoami)@#H: $PWD \"#S\" (#W)#F"

set -g display-panes-time 800 # slightly longer pane indicators display time
set -g display-time 1000      # slightly longer status messages display time

set -g status-interval 10     # redraw status line every 10 seconds

# 24 hour clock
setw -g clock-mode-style 24

# clear both screen and history
bind -n C-l send-keys C-l \; run 'tmux clear-history'

# activity
set -g monitor-activity on
set -g visual-activity on

# -- navigation ----------------------------------------------------------------

# find session
bind C-f command-prompt -p find-session 'switch-client -t %%'

# pane color
#set -g pane-border-fg red
#set -g pane-border-bg default
#set -g pane-active-border-fg red
#set -g pane-active-border-bg "#8FBC8F"
set -g pane-border-style fg=default
set -g pane-border-style bg=default
set -g pane-active-border-style fg=default
set -g pane-active-border-style bg=default

# pane navigation
# default bindings:
# Up, Down, Left, Right
# Change to the pane above, below, to the left, or to the right of the current pane.
# default binding: ';' Move to the previously active pane.
bind k select-pane -U  # move up (prefix k)
bind j select-pane -D  # move down (prefix j)
bind h select-pane -L  # move left (prefix h)
bind l select-pane -R  # move right (prefix l)

# pane resizing
# key: prefix C-[k,j,h,l]
#
# default bindings:
# M-Up, M-Down, M-Left, M-Right
#   Resize the current pane in steps of five cells.
#
# C-Up, C-Down, C-Left, C-Right
#   Resize the current pane in steps of one cell.
bind -r ^k resize-pane -U 5 # upward (prefix Ctrl+k)
bind -r ^j resize-pane -D 5 # downward (prefix Ctrl+j)
bind -r ^h resize-pane -L 5 # to the left (prefix Ctrl+h)
bind -r ^l resize-pane -R 5 # to the right (prefix Ctrl+l)

# Swap the current pane with the previous pane.
# key: prefix <
# default binding: '{'
bind < swap-pane -U

# Swap the current pane with the next pane.
# key: prefix >
# default binding: '}'
bind > swap-pane -D

# window navigation
bind Tab last-window      # move to last active window (prefix Tab)
# default binding: 'l' Move to the previously selected window.

# window split
bind - split-window -c "#{pane_current_path}"    # vertical split (prefix -)
# default binding: '"' Split the current pane into two, top and bottom.
bind | split-window -h -c "#{pane_current_path}" # horizontal split (prefix |)
# default binding: '%' Split the current pane into two, left and right.

# create an new window based on current path
#bind c new-window -c "#{pane_current_path}"

# kill pane & window
bind q killp              # kill pane (prefix q)
# default binding: 'x' Kill the current pane.
bind ^q killw             # kill window (prefix Ctrl+q)
# default binding: '&' Kill the current window.

# -- enable/disable mouse ------------------------------------------------------
bind m run "cut -c3- ~/.tmux.conf | sh -s toggle_mouse"

# -- man page ------------------------------------------------------------------
bind M command-prompt "splitw -h 'exec man %%'"

# -- status line ---------------------------------------------------------------
# Source tmux statusbar config
# the following configuration created by tmuxline.vim of my vim settings
#   see https://github.com/jarsonfang/vimconfig.git
if-shell "test -f ~/.tmux-statusline.conf" "source ~/.tmux-statusline.conf"

# -- enable vim keybindings in copy mode ---------------------------------------
setw -g mode-keys vi

# Enter copy mode, enter copy mode to copy text or view the history.
# default binding: '['
bind Enter copy-mode

# Paste from the top pate buffer, paste the most recently copied buffer of text.
# default binding: ']'
bind P paste-buffer

bind b list-buffers   # list paster buffers
# default binding: '#' List all paste buffers.
# default binding: '=' Choose which buffer to paste interactively from a list.

bind-key -T copy-mode-vi v send-keys -X begin-selection
bind-key -T copy-mode-vi y send-keys -X copy-selection-and-cancel
bind-key -T copy-mode Escape send-keys -X cancel

# -- clipboard -----------------------------------------------------------------
bind C-c run "tmux save-buffer - | xsel -ib"
bind C-v run "tmux set-buffer \"$(xsel -ob)\"; tmux paste-buffer"

# -- frequently used default bindings  -----------------------------------------

# ',' Rename the current window.
# '$' Rename the current session.

# 'w' Choose the current window interactively.
# 's' Select a new session for the attached client interactively.

# 'c' Create a new window.
# 'n' Change to the next window.
# 'p' Change to the previous window.
# '0 to 9' Select windows 0 to 9.
# '.' Prompt for an index to move the current window.
# 'f' Prompt to search for text in open windows.(Note: find windows with the given name)

# 'z' Toggle the current pane zoom.
# '!' Break the current pane out of the window.(Note: make the current pane as a new window.)

# 'd' Detach the current client.
# ':' Enter the tmux command prompt.
# 'C-z' Suspend the tmux client.

# '~' Show previous messages from tmux, if any.
# 't' Show the time.

# ►_◄
#
# # exit the script if any statement returns a non-true return value
# set -e
#
# toggle_mouse() {
#   if (tmux show -g -w |grep -q mode-mouse); then
#     old=$(tmux show -g -w | grep mode-mouse | cut -d' ' -f2)
#     new=""
#
#     if [ "$old" = "on" ] ; then
#       new="off"
#     else
#       new="on"
#     fi
#
#     tmux set -g mode-mouse $new \;\
#          set -g mouse-resize-pane $new \;\
#          set -g mouse-select-pane $new \;\
#          set -g mouse-select-window $new \;\
#          display "mouse: $new"
#   else
#     old=$(tmux show -g | grep mouse | head -n 1 | cut -d' ' -f2)
#     new=""
#
#     if [ "$old" = "on" ] ; then
#       new="off"
#     else
#       new="on"
#     fi
#
#     tmux set -g mouse $new \;\
#          display "mouse: $new"
#   fi
# }
#
# $@

